@echo off
setlocal enabledelayedexpansion

:: 定义相对路径
set "ASSETS_DIR=..\assets\images"
set "CONFIG_DIR=..\lib\generate"

:: 获取命令行传入的参数，默认命名风格为 "underscore" 和默认不添加后缀
set "USE_UNDERSCORE=%1"
if "%USE_UNDERSCORE%"=="" set "USE_UNDERSCORE=underscore"

:: 是否不执行 flutter pub get, 默认为执行(fast_auto_assets.sh)
set "FAST_MODE=%2"

:: 配置文件路径
set "CONFIG_FILE=%CONFIG_DIR%\app_image_path.dart"

:: 创建配置目录
if not exist "%CONFIG_DIR%" mkdir "%CONFIG_DIR%"

:: 创建配置文件
echo // 自动生成的资源配置文件 > "%CONFIG_FILE%"
echo // ignore_for_file: constant_identifier_names >> "%CONFIG_FILE%"
echo abstract class AppImagePath { >> "%CONFIG_FILE%"

:: 处理资产图像的函数
call :process_images "%ASSETS_DIR%" "%FAST_MODE%"

echo <<<<<<<<<<<<<<<<<<<文件生成结束<<<<<<<<<<<<<<<<<<< >> "%CONFIG_FILE%"
echo } >> "%CONFIG_FILE%"

if not "%FAST_MODE%"=="fast" (
    :: 返回上一级目录
    cd ..
    :: 最后再次执行 flutter pub get
    flutter pub get
)

echo 配置文件生成完成: %CONFIG_FILE%

goto :eof

:process_images
:: 处理资产图像
set "dir=%1"
set "add_suffix=%2"

echo >>>>>>>>>>>>>>>>>开始处理目录: %dir%>>>>>>>>>>>>>>>>>>>

:: 遍历指定目录下的所有图像文件
for /r "%dir%" %%f in (*.png *.jpg *.jpeg *.ico *.webp *.heif *.svg *.gif) do (
    set "file=%%f"
    set "relative_path=!file:%dir%\=!"
    set "file_name_without_ext=!relative_path:~0,-4!"

    :: 生成常量名称
    set "constant_name=!file_name_without_ext:\=_!"

    :: 根据命名风格修改常量格式
    if "%USE_UNDERSCORE%"=="camelcase" (
        :: 将下划线分隔的字符串转换为小驼峰命名
        set "constant_name=!constant_name:_=!"
        for /f "tokens=1,* delims=_" %%a in ("!constant_name!") do (
            set "constant_name=%%a"
            set "rest=%%b"
            for %%i in (!rest!) do (
                set "constant_name=!constant_name!!i:~0,1!!i:~1!"
            )
        )
    )

    :: 如果需要添加后缀
    if "%add_suffix%"=="true" (
        if "%USE_UNDERSCORE%"=="underscore" (
            set "constant_name=!constant_name!_suffix"
        ) else (
            set "constant_name=!constant_name!Suffix"
        )
    )

    :: 写入常量定义到配置文件
    echo static const String !constant_name! = 'assets/images/!relative_path!'; >> "%CONFIG_FILE%"
)

goto :eof
